package com.bjpowernode.mvc.settings;

import com.bjpowernode.common.client.RedisClient;
import com.bjpowernode.mvc.interceptor.TokenInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


//全局跨域处理.
@Configuration
public class MvcSettings implements WebMvcConfigurer {
    @Resource
    private RedisClient redisClient;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        TokenInterceptor tokenInterceptor=new TokenInterceptor(redisClient);

        //指定拦截的url地址
        String [] addPath={"/user/realname"," /income/more","/recharge/more","/bid/more","/user/info","/bid/product","/user/logout","/product/detail","/upload/header"};
        registry.addInterceptor(tokenInterceptor).addPathPatterns(addPath);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        System.out.println("===========addCorsMappings===========");
        registry.addMapping("/**")   //拦截的请求url
                .allowedOriginPatterns("*")   //允许的请求来源,发送跨域请求的服务器域名
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")   //允许跨域方式.
                .allowCredentials(true)    //支持cooking
                .maxAge(3600)               //预检请求的有效期,单位秒
                .allowedHeaders("*");         //包含的请求头
    }
}


